package com.soxing.braindata.web.controller.research;

import com.soxing.braindata.dao.domain.request.TesteeSearchRequest;
import com.soxing.braindata.dao.entity.Site;
import com.soxing.braindata.dao.entity.Testee;
import com.soxing.braindata.domain.response.sync.SyncResponse;
import com.soxing.braindata.domain.rpc.OriginReserve;
import com.soxing.braindata.domain.rpc.OriginSite;
import com.soxing.braindata.service.manager.ITesteeManager;
import com.soxing.braindata.service.sync.ISyncService;
import com.soxing.common.utils.response.MyPage;
import com.soxing.common.utils.response.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author :durango
 * @date :2022/6/11 9:45
 * @description 课题被试管理
 */
@Slf4j
@RestController
@RequestMapping("/research/testee")
@Api(value = "课题被试管理", tags = {"课题被试管理"})
public class ResearchTesteeController {
    @Resource
    private ITesteeManager testeeManager;
    @Resource
    private ISyncService<OriginReserve> syncReserveService;

    /**
     * 基于id获取被试信息
     *
     * @param id
     * @return 返回被试信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "基于id获取被试信息")
    public WebResponse<Testee> getById(@PathVariable("id") Integer id) {
        return testeeManager.getById(id);
    }

    /**
     * 基于条件查询被试
     *
     * @param request
     * @return
     */
    @PostMapping("/search")
    @ApiOperation(value = "基于条件查询被试")
    public WebResponse<MyPage<Testee>> search(@RequestBody(required = false) TesteeSearchRequest request) {
        if (request == null) {
            request = new TesteeSearchRequest();
        }
        return testeeManager.search(request);
    }

    @PostMapping
    @ApiOperation(value = "新增课题")
    public WebResponse<Boolean> add(@RequestBody OriginReserve originReserve) {
        SyncResponse res = syncReserveService.syncData(originReserve);
        if (res.getCode() == 0 ) {
            return WebResponse.success("添加成功");
        }else{
            return WebResponse.fail("添加失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "更新课题")
    public WebResponse<Boolean> update(@RequestBody Testee testee) {
        return testeeManager.update(testee);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "基于id删除")
    public WebResponse<Boolean> deleteById(@PathVariable("id") Integer id) {
        return testeeManager.deleteById(id);
    }

}
